package cn.wyl.controller;

import cn.wyl.domain.Order;
import cn.wyl.domain.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/{id}")
    @HystrixCommand(fallbackMethod = "userFallBack")
    public Order get(@PathVariable("id") Long id){
        /**
         * 发起远程调用user_server 微服务 获取user对象
         */
        //String url = "http://localhost:10200/user/1";

        String url = "http://user-server/user/1"; //集群-使用服务名字访问
        User user = restTemplate.getForObject(url, User.class);
        return new Order(id,"202211251555",user);
    }

    public Order userFallBack(@PathVariable("id") Long id){
        return new Order(id,"服务不可用，请稍后再试",null);
    }

}
